package com.gusti.turnos.services;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.gusti.turnos.dao.IAppointmentDao;
import com.gusti.turnos.dao.IEventDao;
import com.gusti.turnos.dao.IUserDao;
import com.gusti.turnos.exception.TurnosException;
import com.gusti.turnos.util.MessageUtil;
import com.gusti.turnos.util.ServerUtil;

/**
 * 
 * @author gusti
 *
 */
public abstract class AbstractService {

	@Autowired
	protected IUserDao userDao;
	@Autowired
	protected IAppointmentDao appointmentDao;
	@Autowired
	protected IEventDao eventDao;

	protected void checkUserId( long userId ) {

		if( userId != ServerUtil.getInstance().getCurrentUserId() ) {
			throw new TurnosException( "Current user is not authorized to access" );
		}
	}

	protected void checkAuthenticationCredentials( String sessionId, String uuid ) {

		if( StringUtils.isBlank( sessionId ) || StringUtils.isBlank( uuid ) ) {
			throw new TurnosException( MessageUtil.getMessage( MessageUtil.ERROR_INVALID_PARAMETERS ) );
		}

		if( !sessionId.equals( ServerUtil.getInstance().getSessionId() ) ) {
			throw new TurnosException( MessageUtil.getMessage( MessageUtil.ERROR_INVALID_SESSION ) );
		}

		if( !uuid.equals( ServerUtil.getInstance().getCurrentUserUuid() ) ) {
			throw new TurnosException( MessageUtil.getMessage( MessageUtil.ERROR_INVALID_USER_UUID ) );
		}
	}

	protected void checkUserAdminLevel() {

		if( !ServerUtil.getInstance().isCurrentUserAdmin() ) {
			throw new TurnosException( MessageUtil.getMessage( MessageUtil.ERROR_AUTHORIZATION_FAILED ) );
		}
	}
}
